<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>栈</title>
    <script src="./栈的实现.js"></script>
</head>
<body>
    <script>
        //1.测试栈的功能
        let stack1 = new Stack();
        console.log(stack1.push(1));
        console.log(stack1.push(2));
        console.log(stack1.push(3));
        console.log(stack1.push(4));
        console.log(stack1.push(5555));
        console.log(stack1);
        console.log(stack1.size());
        console.log(stack1.peek());
        console.log(stack1.isEmpty());
        console.log(stack1.pop());
        console.log(stack1);
        console.log(stack1.toString());



        console.log('-----------------')
        //2.栈实现十进制转二进制
        let decNum = 100;
        
        //实现十进制转二进制的函数
        function dec2bin(decNumber) { 
            //空栈
            let stack2 = new Stack();
            while(decNumber>0){
                //除2取余
                let remainder = decNumber % 2;
                //重新给十进制数赋值
                decNumber = Math.floor(decNumber/2);
                //余数压栈
                stack2.push(remainder);
            };
            //循环结束 依次弹出栈收集二进制结果、
            let binNum = '';
            while (!stack2.isEmpty()){
                binNum += stack2.pop();
            };
            //返回最终结果
            return  binNum
         };
         console.log(`${decNum}这个十进制转二进制的结果${ dec2bin(decNum)}`);
        




    </script>
</body>
</html>